package com.alipay.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayRequest;
import com.alipay.api.AlipayResponse;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.alipay.config.AlipayConfig;
import com.alipay.constant.ProductCode;
import com.alipay.model.request.PcPayRequest;
import com.alipay.model.response.PcPayResponse;
import com.alipay.model.response.WapPayResponse;
import com.alipay.service.AbstractPayService;

/**
 * @author lwj
 * @create 2025-10-11-10:20
 */
public class PcPayServiceImpl extends AbstractPayService<PcPayRequest, PcPayResponse> {

    public PcPayServiceImpl(AlipayClient alipayClient, AlipayConfig alipayConfig) {
        super(alipayClient, alipayConfig);
    }

    @Override
    protected AlipayRequest createAlipayRequest() {
        return new AlipayTradePagePayRequest();
    }

    @Override
    protected void setCallbackUrl(AlipayRequest alipayRequest) {
        // 设置同步与异步的回调地址
        alipayRequest.setNotifyUrl(alipayConfig.getNotifyUrl());
        alipayRequest.setReturnUrl(alipayConfig.getReturnUrl());
    }

    @Override
    protected void setRequestParams(AlipayRequest alipayRequest, JSONObject bizContent) {
        // 强转为具体请求类型
        AlipayTradePagePayRequest pagePayRequest = (AlipayTradePagePayRequest) alipayRequest;
        // 设置请求参数
        pagePayRequest.setBizContent(bizContent.toJSONString());
    }

    @Override
    protected AlipayResponse executeAlipayRequest(AlipayRequest alipayRequest) throws AlipayApiException {
        // 执行请求
        return alipayClient.pageExecute((AlipayTradePagePayRequest) alipayRequest);
    }

    @Override
    protected PcPayResponse handleAlipayResponse(AlipayResponse alipayResponse, PcPayRequest request) {
        PcPayResponse response = new PcPayResponse();
        // 获取响应结果
        AlipayTradePagePayResponse pagePayResponse = (AlipayTradePagePayResponse) alipayResponse;

        // 填充自定义响应对象数据
        return populateResponse(response,pagePayResponse);
    }

    @Override
    protected PcPayResponse createEmptyResponse() {
        return new PcPayResponse();
    }

    @Override
    protected String getProductCode() {
        return ProductCode.PC_PAY;
    }

    @Override
    protected void populateSpecificFields(PcPayResponse response, AlipayResponse alipayResponse) {
        // 强转为PC支付响应
        AlipayTradePagePayResponse pagePayResponse = (AlipayTradePagePayResponse) alipayResponse;
        // 设置订单信息
        response.setOrderHtmlForm(pagePayResponse.getBody());
    }
}